//
//  GJParseScaleUtil.m
//  YDGJ
//
//  Created by 黄卫 on 17/2/24.
//  Copyright © 2017年 huangwei. All rights reserved.
//

#import "TYParseScaleUtil.h"

@implementation WeightInfo

@end

@implementation TYParseScaleUtil

+ (WeightInfo *)parseGoodsNoWithSearchKey:(NSString *)searchKey
{
    if (!searchKey || ![searchKey isKindOfClass:[NSString class]]) {
        return nil;
    }
    if (searchKey.length!=13&&searchKey.length!=18) {
        return nil;
    }
    if (searchKey.length==18) {
        return parseDaHuaGoodsNoBy18(searchKey);
    }
    else if (searchKey.length==13) {
        return parseDaHuaGoodsNoBy13(searchKey);
    }
    return nil;
}

WeightInfo * parseDaHuaGoodsNoBy18(NSString *str)
{
    WeightInfo *weightInfo = [WeightInfo new];
    NSString *shopId = @"",*goodsNo = @"",*money = @"",*weight = @"",*last = @"";
    shopId = [str substringToIndex:2];
    goodsNo = [str substringWithRange:NSMakeRange(2, 5)];
    money = [str substringWithRange:NSMakeRange(7, 5)];
    double trueMoney = money.doubleValue;
    trueMoney = trueMoney/100.f;
    weight = [str substringWithRange:NSMakeRange(12, 5)];
    double trueWeight = weight.doubleValue;
    trueWeight = trueWeight/1000.f;
    last = [str substringWithRange:NSMakeRange(17, 1)];
    double price = trueMoney/trueWeight;
    weightInfo.goodsNo = goodsNo;
    weightInfo.num = [NSString stringWithFormat:@"%.3f",trueWeight];
    weightInfo.total = [NSString stringWithFormat:@"%.2f",trueMoney];
    weightInfo.price = [NSString stringWithFormat:@"%.2f",price];
    return weightInfo;
}

WeightInfo * parseDaHuaGoodsNoBy13(NSString *str)
{
    WeightInfo *weightInfo = [WeightInfo new];
    NSString *shopId = @"",*goodsNo = @"",*weight = @"",*last = @"";
    shopId = [str substringToIndex:2];
    goodsNo = [str substringWithRange:NSMakeRange(2, 5)];
    weight = [str substringWithRange:NSMakeRange(7, 5)];
    double trueWeight = weight.doubleValue;
    trueWeight = trueWeight/1000.f;
    last = [str substringWithRange:NSMakeRange(12, 1)];
    weightInfo.goodsNo = goodsNo;
    weightInfo.num = [NSString stringWithFormat:@"%.3f",trueWeight];
    return weightInfo;
}

@end
